package com.frame.work.frame.deal.device0fab;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblDeviceActions;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *四路联控面板0FAB0602
 */
@Data
public class SiLuLianKongMianBan_0FAB0602 extends BaseFrame {

    private String  spreadingType;
    private String  switchState;




    public SiLuLianKongMianBan_0FAB0602(String gatewayMac, String data){

        //将基础数据进行处理
        super(gatewayMac,data);


        //处理此设备特有的数据
        this.setSpreadingType(StringUtils.substring(data, 38, 40));
        String  switchState_1=(StringUtils.substring(data, data.length() - 8, data.length() - 6));
        String  switchState_2=(StringUtils.substring(data, data.length() - 6, data.length() - 4));
        String  switchState_3=(StringUtils.substring(data, data.length() - 4, data.length() - 2));
        String  switchState_4=(StringUtils.substring(data, data.length() - 2, data.length() ));


        //使用数组配合for循环
        String [] switchState = {"0",switchState_1,switchState_2,switchState_3,switchState_4};

        String nightLightTime=null;
        //查询数据库并给nightLightTime赋值

        SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
        List<TblDeviceActions> list = testMapper.getDevice2(this.getDeviceMac(),this.getOdIndex(),this.getDeviceType(),this.getProductType());

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getActionsKey() == "nightLightTime") {
                nightLightTime = (String) list.get(i).getActionsValue();
            }
        }
        if (nightLightTime!=null){
            //通过for循环进行4次发送数据
            String productType =this.getProductType();

            for (int i=1;i<5;i++){

                this.setProductType( productType+ "_"+i);
                this.setSwitchState(switchState[i]);

                //将actions信息放入json对象中
                Map<String,Object> actions = new HashMap();
                actions.put("switchState",switchState[i]);
                actions.put("nightLightTime",nightLightTime);
                this.setActions(actions);



                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.sendMQTT(gatewayMac,json);


            }
        }else{
            //通过for循环进行4次发送数据
            String productType =this.getProductType();

            for (int i=1;i<5;i++){

                this.setProductType( productType+ "_"+i);
                this.setSwitchState(switchState[i]);

                //将actions信息放入json对象中
                Map<String,Object> actions = new HashMap();
                actions.put("switchState",switchState[i]);
                this.setActions(actions);



                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianKongDevice(gatewayMac,json);

            }
        }







    }


}
